<?php
include_once('/var/www/html/new/com.inc.php');



// if($_REQUEST['ac']=='')   exit('{"code":"1011","msg":"请输入授权账号"}');
// if($_REQUEST['pw']=='')   exit('{"code":"1011","msg":"请输入授权密码"}');

if($_REQUEST['sid']=='')  exit('{"code":"1011","msg":"请输入景区ID"}');
if($_REQUEST['code']=='') exit('{"code":"1011","msg":"请输入凭证码"}');
if(md5($_REQUEST['sid'].$_REQUEST['code'].'PFT12301')!=$_REQUEST['token']) exit('{"code":"1011","msg":"认证失败"}');


$salerid = $_REQUEST['sid'];  // 商户ID
$voucher = $_REQUEST['code']; // 凭证码



// $salerid = 602288;  // 商户ID
// $voucher = '164393';// 凭证码


$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));


// 获取订单信息部分
$verify = new PsVerfiy($s, $salerid);
if(strlen($voucher)==6){// 凭证码
	$code = $voucher;
	$ordernum = $verify->reverse($voucher);// 反查订单号
    $send = array();
    echo json_encode($verify->information($ordernum));
}elseif(strlen($voucher)==11 && is_numeric($voucher)){// 手机号

	$mobile = $voucher;
	$send = array();
	$status = ($_SESSION['sid']==1) ? '0|2':'0';// 0 未验证 2 已过期
	$xmlStr = $s->Order_Globle_Search($salerid,'','','','','','','','','','','','','',$mobile, $status,'','',1,0,0,100,'','','','','','','','','',0,0);
	$simxml = simplexml_load_string($xmlStr);
	$unique_a = array();// 多级转分校重复
	$unique_b = array();// 子票防止重复
	foreach($simxml->Rec as $row){
		
		if(isset($unique_a[(string)$row->UUordernum])) continue;
		$unique_a[(string)$row->UUordernum] = (string)$row->UUordernum;	
		
		if((string)$row->UUpack_order!=0){
			if(isset($unique_b[(string)$row->UUpack_order])) continue;
			$unique_b[(string)$row->UUpack_order] = (string)$row->UUpack_order;	
		}
		$send = array_merge($send, $verify->information((string)$row->UUordernum));
	}
	echo json_encode($send);
	
}else{// 身份证

	$idCard = $voucher;
	$simxml = $verify->checkIDCard('', $voucher);
	$simxml_o = simplexml_load_string($simxml);
	$send = array();
	$unique = array();// 子票防止重复
	foreach($simxml_o->Rec as $row){
		if((string)$row->UUpack_order!=0){
			if(isset($unique[(string)$row->UUpack_order])) continue;
			$unique[(string)$row->UUpack_order] = (string)$row->UUpack_order;	
		}
		$send = array_merge($send, $verify->information((string)$row->UUordernum));
	}
	echo json_encode($send);
	
}




class PsVerfiy{
	
	public $inside = null;// 内部接口
	public $salerid = 0;  // 商户账号
	public $errCode = array(
		'105'=>'找不到订单',
		'121'=>'订单状态出错,订单已验证或已过期',
		'119'=>'订单状态出错,已取消或其他状态',
		'521'=>'验证间隔时间未到',
		'133'=>'游玩时间未到',
		'134'=>'游玩时间已过',
		'118'=>'终端服务器无响应',
		'1070'=>'订单未支付',
	);

	
	public function __construct($s, $salerid){
		$this->inside = $s;
		$this->salerid = $salerid;
	}
	
	// 验证
	public function check($voucher, $rtime=''){
		return $this->inside->Terminal_Check_In($this->salerid, $voucher, $rtime);
	}
	
	
	
	// 身份证获取所有订单信息
	public function checkIDCard($username, $idCard=null, $rtime=null){

		return $this->inside->Terminal_Check_IDcard($this->salerid, $username, $idCard, $rtime);
	}
	
	// 通过商户6位账号和凭证码反查订单
	public function reverse($voucher, $sid=0){
		
		$salerid = $this->salerid;
		if($sid==0) $sid = $_SESSION['sid'];
		$ordernum = $this->inside->Code_Verify_Order($salerid, $voucher, $sid);
        return $ordernum;
	}
	

	
	// 获取订单信息
	public function information($orderId='', $mobile=''){
		
		$init_order = $orderId;
		$sendData = array();
		$salerid = $this->salerid;
		$orderStatus = ($_SESSION['sid']==1) ? '0|2':'0';// 0 未验证 2 已过期
		if($orderId=='') return $sendData;
		$xmlStr = $this->inside->Order_Globle_Search($salerid,'','','','','','','','','','','',$orderId,'','', $orderStatus,'','',1,0,0,1,'','','','','','','','','',0,0);

		$simxml = simplexml_load_string($xmlStr);
		// 订单查找可能联票只能再次获取
		if($orderId!='' &&(string)$simxml->Rec->UUconcat_id==(string)$simxml->Rec->UUordernum){
			$xmlStr = $this->inside->Order_Globle_Search($salerid,'','','','','','','','','','','',$orderId,'',$mobile, $orderStatus,'','',1,0,0,100,'','','','','','','','','',1,0);
			$simxml = simplexml_load_string($xmlStr);
		}
		// 套票可能同一产品的两个不同子票会一起验证，所以都要显示出来
		if((string)$simxml->Rec->UUpack_order!=0){
			if((string)$simxml->Rec->UUstatus!=0) return $sendData;
			$orderId = (string)$simxml->Rec->UUpack_order;
			$xmlStr = $this->inside->Order_Globle_Search($salerid,'','','','','','','','','','','',$orderId,'',$mobile, $orderStatus,'','',1,0,0,100,'','','','','','','','','',0,2);
			$simxml = simplexml_load_string($xmlStr);
		}

		$sets_f = array();
		$same_f = 0;
		$isFirst = true;
		foreach($simxml->Rec as $order){
			
			if(in_array((string)$order->UUordernum, $sets_f)) continue;// 已经设置过，防止admin重复显示

			if((string)$order->UUpack_order!=0){// 套票
				// 非第一次设置并且lid不跟第一样，并且不是这次查找的订单
				if($same_f!=0 && (string)$order->UUlid!=$same_f 
					&& (string)$order->UUordernum!=$init_order) continue;// 不是统一产品底下的两个产品不显示
				
				if($order->UUstatus!=0) continue;// 顺序问题
				$same_f = (string)$order->UUlid;
				// if((string)$order->UUp_type=='C'){
					if($isFirst){// 酒店
						$orderId = (string)$order->UUordernum;
						$isFirst = false;
					}
				// }else{
					// $orderId = (string)$order->UUordernum;
				// }
				// 第二次是要的的订单，说明之前的跟这次不一样，清空
				if(!$isFirst && (string)$order->UUordernum==$init_order){
					$sendData = array();
				}
			}elseif($order->UUconcat_id!=0){// 联票
				$concat = (string)$order->UUconcat_id;
				$ordernum = (string)$order->UUordernum;
				$orderId = ($concat==$ordernum) ? $ordernum:$concat;
				if($concat!=$ordernum && !isset($sendData[$orderId]['ordernum'])) continue;
			}else{
				
				$orderId = (string)$order->UUordernum;
			}

			
			if(!isset($sendData[$orderId]['ordernum'])){
				$sendData[$orderId]['ordernum'] = $orderId;//  订单号
				$sendData[$orderId]['ordername'] = (string)$order->UUordername;// 取票人姓名
				$sendData[$orderId]['ordertel'] = (string)$order->UUordertel;// 取票人手机
				$sendData[$orderId]['ordertime'] = (string)$order->UUordertime;// 下单时间
				$sendData[$orderId]['begintime'] = (string)$order->UUbegintime;// 游玩时间
				$sendData[$orderId]['pmode'] = (string)$order->UUpmode;
				$sendData[$orderId]['status'] = (string)$order->UUstatus;
				$sendData[$orderId]['code'] = (string)$order->UUcode;
				$sendData[$orderId]['paystatus'] = (string)$order->UUpaystatus;
				$sendData[$orderId]['ptype'] = (string)$order->UUp_type;
				$sendData[$orderId]['landid'] = (string)$order->UUlid;
				$sendData[$orderId]['checktime'] = (string)$order->UUdtime;// 完成时间
				$sendData[$orderId]['endtime'] = (string)$order->UUendtime;// 完成时间
				$sendData[$orderId]['series'] = (string)$order->UUseries;// 完成时间
				
				
				// 已过期的订单从 2015-06-30 开始不会显示已过期，订单显示 未使用 abc-2015-07-01
				if((int)$order->UUstatus==0){
					if((strtotime((string)$order->UUendtime)+86400)<time())
						$sendData[$orderId]['status'] = 2;
				}
				
				// 对于admin 过期产品返回使用开始时间
				if($_SESSION['sid']==1 && $sendData[$orderId]['status']){
					$sendData[$orderId]['endtime'] = (string)$order->UUendtime;
				}
				
				// 套票不允许改单验证
				if((string)$order->UUpack_order!=0){
					$sendData[$orderId]['ptype'] = 'F';
				}
			}
			
			$sendData[$orderId]['tickets'][] = array(
				'id' => (string)$order->UUtid,
				'name'=>(string)$order->UUttitle,
				'tnum'=>(int)$order->UUtnum,
				'ordernum'=>(string)$order->UUordernum,
				'tprice'=>(string)$order->UUtprice
			);
			$sets_f[] = (string)$order->UUordernum;
		}
		$sendData = array_values($sendData);
		return $sendData;
	}

}




